<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-12 15:43:11 +0200 (Sat, 12 Jul 2008) $
 * $Rev: 1291 $
 * $Author: gavinspearhead $
 * $Id: ajax_admincommands.php 1291 2008-07-12 13:43:11Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathad = realpath(dirname(__FILE__));

require_once "$pathad/../functions/ajax_includes.php";
require_once "$pathad/../functions/pref_functions.php";

if (!$isadmin)
	fatal_error($LN['error_noadmin'] );

// Process commands:
// Needs to be updated for Urdd:
if (isset($_POST['action'])) {
	$prefs = load_config($db);
	$action = $_POST['action'];

	switch ($action) {
	case 'checkversion':
		$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);
		if ($uc->is_connected()) 
			$uc->check_version();
		usleep(500000);
		break;
	case 'poweron' :
		start_urdd();
        break;
    case 'restart':
		$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);
		if ($uc->is_connected()) 
			$uc->restart_urdd();
		usleep(500000);
		break;
	case 'shutdown' :
		stop_urdd();
		break;
	case 'clear' : 
		$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);
		if ($uc->is_connected()) 
			$uc->cancel('all');
		usleep(500000);
		break;
	case 'removeready' : 
		$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);
		if ($uc->is_connected()) 
			$uc->cleandb();
		usleep(500000);
		break;
	case 'cleandb' : 
		$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);
		if ($uc->is_connected()) 
			$uc->cleandb('all');
		usleep(500000);
		break;
	case 'pause' : 
		$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);
		if ($uc->is_connected()) 
			$uc->pause('all');
		usleep(500000);
		break;
	case 'continue' : 
		$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);
		if ($uc->is_connected()) 
			$uc->continue_cmd('all');
		usleep(500000);
		break;
	case 'ratelimit': // XXX Not implemented yet
	default:
		fatal_error ($LN['error_novalidaction']);
		break;
	}
}
echo 'OK';
?>
